import { FC, ReactNode } from 'react';
import { Navigate } from 'react-router-dom';
import useUserStore from '@/store/user.ts';

interface AuthRouteProps {
  children: ReactNode;
}

const AuthRoute: FC<AuthRouteProps> = ({ children }) => {
  // 假设 useGlobalStore 中存储了用户的登录状态
  const isLoggedIn = useUserStore((state) => state.token);
  console.log(isLoggedIn, 'isLoggedIn');
  const location = useLocation(); // 如果是登录页面，直接渲染子组件
  if (location.pathname === '/Login') {
    return <>{children}</>;
  }

  // 如果用户未登录，重定向到登录页面
  if (!isLoggedIn) {
    return <Navigate to="/Login" replace />;
  }

  // 如果用户已登录，渲染子组件
  return <>{children}</>;
};

export default AuthRoute;
